package com.lancoo.znbkxx.teacher.adapter

import android.graphics.Bitmap
import android.net.Uri
import android.view.View
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.item_preview.view.*
import android.media.MediaMetadataRetriever
import android.os.Handler
import android.os.Message
import android.provider.MediaStore
import android.util.Log
import android.widget.ImageView
import com.lancoo.znbkxx.teacher.help.GlideRoundTransform
import com.lancoo.znbkxx.teacher.utils.DateUtils
import com.lancoo.znbkxx.teacher.utils.StringUtils.modifyTextViewDrawable
import java.util.*
import kotlin.collections.HashMap


/**
 * @author : Admin
 * @date   : 2019/8/13 10:58
 * @desc   :
 */
class GridMediaAdapter(
    layoutResId: Int,
    data: MutableList<LocalMedia>,
    var hashMap: HashMap<String, Any>?
) :
    BaseQuickAdapter<LocalMedia, BaseViewHolder>(layoutResId, data) {
    var state: Int = 0

    companion object {
        const val TYPE_CAMERA = 1
        const val TYPE_PICTURE = 2
    }

    override fun convert(helper: BaseViewHolder, item: LocalMedia?) {
        val position = helper.layoutPosition
        with(helper.itemView) {
            if (helper.itemViewType == TYPE_CAMERA) {
                iv_preview.visibility = View.GONE
                if (state == 0) {
                    this.visibility = View.VISIBLE
                    tv.visibility = View.VISIBLE
                    iv_add.visibility = View.VISIBLE
                } else {
                    this.visibility = View.INVISIBLE
                }
            } else {
                tv.visibility = View.GONE
                iv_add.visibility = View.GONE
                iv_preview.visibility = View.VISIBLE
                if (state == 0) {
                    iv_delete.visibility = View.VISIBLE
                    helper.addOnClickListener(R.id.iv_delete)
                } else {
                    iv_delete.visibility = View.INVISIBLE
                }
                val media = data[position]
                val mimeType = media.mimeType
                val path = if (media.isCompressed) media.compressPath else media.path
                val duration = media.duration
                tv_duration.visibility =
                    if (mimeType.contains("image")) View.GONE else View.VISIBLE
                iv_play.visibility =
                    if (mimeType.contains("image")) View.GONE else View.VISIBLE
                if (mimeType.contains("audio")) {
                    val drawable = ContextCompat.getDrawable(context, R.mipmap.picture_audio)
                    modifyTextViewDrawable(tv_duration, drawable!!, 0)
                } else if (mimeType.contains("video")) {
                    val drawable = ContextCompat.getDrawable(context, R.mipmap.video_icon)
                    modifyTextViewDrawable(tv_duration, drawable!!, 0)
                }
                tv_duration.text = DateUtils.timeParse(duration)
                if (mimeType.contains("audio")) {
                    iv_preview.setImageResource(R.drawable.audio_placeholder)
                } else {
                    val uri = Uri.parse(if (path.contains("storage")) "file://$path" else path)
                    if (mimeType.contains("video")) {
                        val time = hashMap?.get("videoTimes") as? Long
                        tv_duration.text = DateUtils.timeParse(time ?: 0)
                        val corners = GlideRoundTransform(context, 6)
                        val bitmap = hashMap?.get("bitmap") as? Bitmap
                        if (null != bitmap) {
                            Glide.with(mContext).load(
                                Uri.parse(
                                    MediaStore.Images.Media.insertImage(
                                        mContext.contentResolver,
                                        bitmap,
                                        "IMG" + Calendar.getInstance().time,
                                        null
                                    )
                                )
                            ).apply(
                                RequestOptions()
                                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                                    .transform(corners)
                            ).into(iv_preview)
                        }else{

                        }
                    } else {
                        val corners = GlideRoundTransform(context, 6)
                        Glide.with(context).load(uri).apply(
                            RequestOptions()
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .transform(corners)
                        ).into(iv_preview)
                    }
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (isShowAddItem(position)) {
            TYPE_CAMERA
        } else {
            TYPE_PICTURE
        }
    }

    override fun getItemCount(): Int {
        return if (data.size < 5) {
            data.size + 1
        } else {
            data.size
        }
    }

    private fun isShowAddItem(position: Int): Boolean {
        val size = data.size
        return position == size
    }

    fun updateState(state: Int) {
        this.state = state
        notifyDataSetChanged()
    }

    fun updateMap(map: HashMap<String, Any>?){
        this.hashMap=map
        notifyDataSetChanged()
    }
}